package com.example.qcweb;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dao.UserAssignService;
import com.example.dao.UserService;
import com.example.modal.User;
import com.example.modal.WorkingProcedure;
import com.example.qc.R;

import java.text.SimpleDateFormat;

public class signtoaddactivity extends TabActivity {

    private Button btcancel, btsign, btcancel1, btsign1;
    private EditText etname, etpwd;
    private ImageView igewm;
    private TabHost tabHost = null;
    private WorkingProcedure workProcedure;
    private String varTableType, lastSeqNumber;
    private String uname, upwd;
    private UserAssignService uService;
    private UserService userService;
    private String controlname, userrealname;
    private boolean flag = false;// 系统是否存在该用户
    private int usertype;// 用户类型
    static final String User_ACTION = "com.gobackBroadCastReceiver.Action";
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 调用TabActivity的getTabHost()方法获取TabHost对象
        // setContentView(R.layout.activity_sign);
        user = MyGlobal.user;
        Log.i("usertyep", user.getUsername());
        setwindowsize();
        getwindowTab();
        getparameter();
        findviewControl();
        btcontrolsetonclicker();
    }

    private void btcontrolsetonclicker() {
        // TODO Auto-generated method stub
        btsign.setOnClickListener(clickHandler);
        btcancel.setOnClickListener(clickHandler);
        btsign1.setOnClickListener(clickHandler);
        btcancel1.setOnClickListener(clickHandler);
    }

	/*  private BroadcastReceiver mydynamic = new BroadcastReceiver() {   
          
	        public void onReceive(Context context, Intent intent) {   
	            String action = intent.getAction();   
	            Toast.makeText(context, "动态MyReceive接收到:"+action, 1000).show(); 
	            Log.i("MyReceive_dynic","current priority is 10");
	            Bundle b=new Bundle();
	            b.putString("name", "hyl");
	            setResultExtras(b);
	           
	        }   
	    };   */

    private void passresult(int uflag) {
	    	/*/数据是使用Intent返回
            Intent intent = new Intent();
            //把返回数据存入Intent
            intent.putExtra("result", "My name is linjiqin");
            //设置返回数据
            OtherActivity.this.setResult(RESULT_OK, intent);
            //关闭Activity
            OtherActivity.this.finish();*/
        Intent myintent = new Intent();
        myintent.putExtra("uflag", uflag);
        setResult(Activity.RESULT_OK, myintent);
        //signtoaddactivity.this.finish();

    }

    View.OnClickListener clickHandler = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
                case R.id.btsign:
                    uname = etname.getText().toString();
                    upwd = etpwd.getText().toString();
                    flag = new UserService(signtoaddactivity.this).login(uname,
                            upwd);
                    usertype = new UserService(signtoaddactivity.this)
                            .findUserType(uname, upwd);
				/*userrealname = new UserService(signtoaddactivity.this)
						.getUserRealName(uname, upwd);*/
                    uService = new UserAssignService(signtoaddactivity.this);
                    int uflag = 0;
                    if (flag & usertype == 4) // 如果用户存在,是工艺员
                    {
                        uflag = 1;
                        Log.i("usertype2", user.getUsername());
                        passresult(uflag);
					/*if (varTableType.equalsIgnoreCase("partitem")) {
						DBDao.addMPI(workProcedure, lastSeqNumber);
						passresult(uflag);
					

					} else if (varTableType.equalsIgnoreCase("ExamItem")) {
						DBDao.addEI(workProcedure, lastSeqNumber);
						passresult(uflag);

					} else if (varTableType.equalsIgnoreCase("MultiItem")) {
						DBDao.addMD(workProcedure, lastSeqNumber);
						passresult(uflag);

					}*/
                    } else {
                        Toast.makeText(signtoaddactivity.this,
                                "该用户没有操作权限，请用工艺员账户登录！", Toast.LENGTH_LONG).show();
                        uflag = 0;
                        passresult(uflag);
                    }
                case R.id.btcancl:
                    signtoaddactivity.this.finish();
                    break;

                case R.id.btcancel1:
                    signtoaddactivity.this.finish();
                    break;
            }
        }
    };

    private void setwindowsize() {
        // 自定义窗口大小
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 0.47); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的0.65
        // 设置窗口显示位置
        p.x = -30;
        p.y = -500;
        getWindow().setAttributes(p);

    }

    private void getwindowTab() {
        TabHost tabHost = getTabHost();
        // 设置使用TabHost布局
        LayoutInflater.from(this).inflate(R.layout.activity_signtoadd,
                tabHost.getTabContentView(), true);
        // 添加第一个标签页
        tabHost.addTab(tabHost.newTabSpec("账户登录").setIndicator("账户修改")
                .setContent(R.id.layout01));
        // 添加第二个标签页,并在其标签上添加一个图片
        tabHost.addTab(tabHost.newTabSpec("二维码登录").setIndicator("二维码登录修改")
                .setContent(R.id.layout02));
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0)
                .findViewById(android.R.id.title);
        tv.setTextSize(16);
        TextView tv1 = (TextView) tabHost.getTabWidget().getChildAt(1)
                .findViewById(android.R.id.title);
        tv1.setTextSize(16);
    }

    private void findviewControl() {
        etname = (EditText) findViewById(R.id.edusername);
        etpwd = (EditText) findViewById(R.id.edpassword);
        btcancel = (Button) findViewById(R.id.btcancel);
        btsign = (Button) findViewById(R.id.btsign);

        igewm = (ImageView) findViewById(R.id.igewm);

        btcancel1 = (Button) findViewById(R.id.btcancel1);
        btsign1 = (Button) findViewById(R.id.btsign1);

    }

    private void getparameter() {
        // TODO Auto-generated method stub
		/*
		 * myAssiIntent.putExtra("key", workProcedure);
		 * myAssiIntent.putExtra("varTableType", varTableType);
		 * myAssiIntent.putExtra("lastSeqNumber", lastSeqNumber);
		 */
        Bundle mybuddle = this.getIntent().getExtras();
        workProcedure = (WorkingProcedure) mybuddle.getSerializable("key");
        lastSeqNumber = mybuddle.getString("lastSeqNumber");
        varTableType = mybuddle.getString("tabletype");
        // Log.i("参数信息", systemid + activityname + controlname);
    }

    private String getDate() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new java.util.Date());
        return userrealname + " " + date;

    }
}
